package priv.zgh.forum.game.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import priv.zgh.forum.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@CrossOrigin
@RestController
@RequestMapping("/game")
public class GameController {

    @ApiOperation("游戏文件上传")
    @PostMapping("/update")
    public R updateGame(@RequestPart(name = "file",required = false) MultipartFile file, @RequestPart(name = "files",required = false) MultipartFile[] files) throws IOException {
        boolean isReceive = false;
        if(file != null){
            isReceive = true;
            System.out.println("接收到上传文件:" + file.getOriginalFilename());
            //获取上传时的文件名(包括扩展名),就是文件的名称
            String originalFileName = file.getOriginalFilename();
            System.out.println(originalFileName);
            //调用次方法就可以直接保存到指定的文件夹下
            file.transferTo(new File("E:/上传的文件/" + originalFileName));
        }
        if(files != null && files.length != 0){
            System.out.println("接收到上传文件列表信息:");
            isReceive = true;
            for(MultipartFile item:files){
                System.out.println(item.getOriginalFilename());
                String originalFilename = item.getOriginalFilename();
                System.out.println(originalFilename);
                item.transferTo(new File("E:/上传的文件/" + originalFilename));
            }
        }
        if(!isReceive){
            return R.error().message("接收文件为空!");
        }
        return R.ok().message("上传成功!");
    }

    @GetMapping("/download")
    public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
        String fileName = "download.txt";// 文件名
        if (fileName != null) {
            //设置文件路径
            File file = new File("E:\\下载目录\\" + fileName);
            //File file = new File(realPath , fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally { // 做关闭操作
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "下载失败";
    }

}
